package com.qububu.play.dineTogether.control;

import static com.qububu.utils.ParamConverter.assertNotEmpty;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qububu.common.user.dto.UserInfoDto;
import com.qububu.play.dineTogether.dto.ActionInfoDto;
import com.qububu.play.dineTogether.dto.PositionDto;
import com.qububu.play.dineTogether.service.DineService;
import com.qububu.utils.DateUtil;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemConstants;
import com.qububu.utils.SystemUtil;

@Controller
@RequestMapping("/play")
public class DineControl {

	@Resource
	private DineService dineService;
	
	@RequestMapping("/dine")
	public String infoDine() {
		return "dine";
	}
	
	@RequestMapping("/dine/dineActivity")
	public String dineActivity(HttpServletRequest req) {

		ActionInfoDto actionInfoDto = dineService.getOngoingOrNearestInfo();
		
		req.setAttribute("actionInfo", actionInfoDto);
		return "dineActivity";
	}
	@RequestMapping("/sportsBa")
	public String sportsBa() {		
		return "sportsBa";
	}
	
	@RequestMapping("/sportsActivity")
	public String sportsActivity() {		
		return "sportsActivity";
	}
	
	/**
	 * 给出正在进行的活动的数据
	 * */
	@RequestMapping("/dine/getNowAction")
	@ResponseBody
	public Map<String, Object> getOngoingOrNearestInfo() {
	
		ActionInfoDto actionInfoDto = dineService.getOngoingOrNearestInfo();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		resultMap.put("action", actionInfoDto);
		
		return resultMap;
	}
	
	/**
	 * @功能 根据action_id修改活动内容
	 */
	@RequestMapping("/dine/modifyActionInfo")
	@ResponseBody
	public Map<String, Object> modifyActionInfo(HttpServletRequest request, HttpServletResponse response){
		
		Map<String, Object> actionMap = new HashMap<String, Object>();
		
		String actionId = assertNotEmpty(request.getParameter("actionId"));
		
		String actionTitle = SystemUtil.convertSpecialMark(assertNotEmpty(request.getParameter("actionTitle")));
		String actionPicUrl = assertNotEmpty(request.getParameter("actionPicUrl"));
		String content = SystemUtil.removeAllScriptFromHtml(assertNotEmpty(request.getParameter("content")));
		String useMoney = ParamConverter.assertNumberString(assertNotEmpty(request.getParameter("useMoney")));
		String beginTime = ParamConverter.assertIsDateString(assertNotEmpty(request.getParameter("beginTime")));
		String endTime = ParamConverter.assertIsDateString(assertNotEmpty(request.getParameter("endTime")));
		String peopleNum = ParamConverter.assertNumberString(assertNotEmpty(request.getParameter("peopleNum")));
		
//		String authorName = assertNotEmpty(request.getParameter("authorName"));
		UserInfoDto userInfo = (UserInfoDto) request.getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME);
		String authorName = userInfo.getNickName();
		
		String positionId = assertNotEmpty(request.getParameter("positionId"));
		
		ActionInfoDto actionInfoDto = createActionInfoEntity(actionId, actionTitle, actionPicUrl, content, useMoney, beginTime, endTime, peopleNum, authorName, positionId);
		
		if(dineService.doModifyActionInfoById(actionInfoDto)) {
			actionMap.put("result", SystemConstants.AJAX_SUCCESS);
		} else {
			actionMap.put("result", SystemConstants.AJAX_FAILED_ALERT);
			actionMap.put("message", "缓存操作失败, 请稍后重试");
		}
		
		return actionMap;
	}
	
	/**
	 * @功能　从action_info中查询过期的活动列表
	 */
	@RequestMapping("/dine/getOutdatedActions")
	@ResponseBody
	public Map<String, Object> getOutdatedActions(HttpServletRequest request, HttpServletResponse response){
		
		List<ActionInfoDto> actionList = dineService.getActionInfoList();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		resultMap.put("actions", actionList);
		
		return resultMap;
	}
	
	/**
	 * @功能　从action_info中查询活动信息
	 * @备注　如果当前时间段有活动则查出活动,如果没有活动则返回null
	 */
	@RequestMapping("/dine/ajaxOngoingOrNearestAction")
	@ResponseBody
	public Map<String, Object> queryOngoingOrNearestInfo(HttpServletRequest request, HttpServletResponse response){
		ActionInfoDto actionInfo = dineService.getOngoingOrNearestInfo();
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("actionInfo", actionInfo);
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		
		return resultMap;
	}
	
	/**
	 * @功能 创建actionInfoDto类
	 * @return
	 */
	private ActionInfoDto createActionInfoEntity(String actionId,
			String actionTitle, String actionPicUrl, String content,
			String useMoney, String beginTime, String endTime,
			String peopleNum, String authorName, String positionId) {
		
		ActionInfoDto actionInfoDto = new ActionInfoDto(actionId);
		PositionDto positionDto = new PositionDto(positionId);
		int publishTime =DateUtil.convertCurrentDTTMtoInt();				
		int beginTimeInt = DateUtil.decodeFromDTString(beginTime);
		int endTimeInt = DateUtil.decodeFromDTString(endTime);
		
		actionInfoDto.setActionTitle(actionTitle);
		actionInfoDto.setActionPicUrl(actionPicUrl);
		actionInfoDto.setContent(content);
		actionInfoDto.setUseMoney(ParamConverter.convertToInteger(useMoney));
		actionInfoDto.setBeginTime(beginTimeInt);
		actionInfoDto.setEndTime(endTimeInt);
		actionInfoDto.setPublishTime(publishTime);
		actionInfoDto.setPeopleNum(ParamConverter.convertToInteger(peopleNum));
		actionInfoDto.setAuthorName(authorName);
		actionInfoDto.setPosition(positionDto);
		
		return actionInfoDto;
	}

	/************************************管理员专用****************************************/
	@RequestMapping("/dine/admin/adminDine")
	public String adminDineTogether() {
		
		return "adminDineTogether";
	}
	
	/**
	 * 发布action入口
	 * */
	@RequestMapping("/dine/admin/publishAction")
	public String addAction(){
		
		return "publishDine";
	}
	
	/**
	 * @功能 创建活动
	 */
	@RequestMapping("/dine/admin/addActionApply")
	public String addActionApply(HttpServletRequest request, HttpServletResponse response){
		
		String actionId = SystemUtil.getUUIDString();
		String actionTitle = SystemUtil.convertSpecialMark(assertNotEmpty(request.getParameter("actionTitle")));
		String actionPicUrl = assertNotEmpty(request.getParameter("actionPicUrl"));
		String content = SystemUtil.removeAllScriptFromHtml(assertNotEmpty(request.getParameter("content")));
		String useMoney = ParamConverter.assertNumberString(assertNotEmpty(request.getParameter("useMoney")));
		String beginTime = ParamConverter.assertIsDateString(assertNotEmpty(request.getParameter("beginTime")));
		String endTime = ParamConverter.assertIsDateString(assertNotEmpty(request.getParameter("endTime")));
		String peopleNum = ParamConverter.assertNumberString(assertNotEmpty(request.getParameter("peopleNum")));
		
//		String authorName = ParamConverter.assertNotEmpty(request.getParameter("authorName"));
		UserInfoDto userInfo = (UserInfoDto) request.getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME);
		String authorName = userInfo.getNickName();
				
		
		/**
		 * 位置服务(小区)
		 */
		String positionId = assertNotEmpty(request.getParameter("positionId"));
		
		ActionInfoDto actionInfoDto = createActionInfoEntity(actionId, actionTitle, actionPicUrl, content, useMoney, beginTime, endTime, peopleNum, authorName, positionId);
		
		if (dineService.doAddActionApply(actionInfoDto)) {
			return "redirect:/play/main";
		}
		
		return "redirect:/play/dine/admin/publishAction";
	}
	
}
